package cn.demoncat.util.mybatis.plus.config;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.LocalCacheScope;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * Mybatis Plus 配置
 *
 * @author 延晓磊
 * @since 2021年04月01日
 */
// 扫描Mapper包（避免与其它依赖的包冲突，强制校验@Mapper注解）
@MapperScan(basePackages = { "cn.demoncat.**.mapper", "cn.centec.**.mapper" }, annotationClass = Mapper.class)
// 支持事务注解
@EnableTransactionManagement
// 配置类(spring.xml-beans)
@Configuration
public class MybatisPlusConfig {

    /**
     * 修改Mybatis全局配置
     *
     * @return
     */
    @Bean
    public ConfigurationCustomizer mybatisPlusConfigurationCustomizer() {
        return configuration -> {
            // 关闭Mybatis缓存
            configuration.setLocalCacheScope(LocalCacheScope.STATEMENT);
            configuration.setCacheEnabled(false);
        };
    }

    /**
     * 修改MybatisPlus全局配置
     *
     * @return
     */
    @Bean
    public MybatisPlusPropertiesCustomizer mybatisPlusPropertiesCustomizer() {
        return properties -> {
            // 关闭Banner
            properties.getGlobalConfig().setBanner(false);
            // 启用SqlRunner
            properties.getGlobalConfig().setEnableSqlRunner(true);
            // 主键类型：AUTO 数据库自增（Mysql），INPUT 自增或指定（Mysql），ASSIGN_ID 全局分配（默认，Oracle）
            properties.getGlobalConfig().getDbConfig().setIdType(IdType.INPUT);
        };
    }

    /**
     * 插件管理
     *
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 动态表名
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> DynamicTableHolder.get(tableName));
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        // 分页
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        // 乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    /**
     * SQL注入器
     *
     * @return
     *
     * @see <a href="https://gitee.com/baomidou/mybatis-plus-samples/tree/master/mybatis-plus-sample-deluxe">自定义Mapper</a>
     *
     * @see <a href="https://mp.baomidou.com/guide/sql-injector.html">Sql注入器</a>
     */
    @Bean
    public ISqlInjector mybatisPlusSqlInjector(){
        return new MybatisPlusSqlInjector();
    }

}
